<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>digichef.recommender.utils</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

      <th class="navbar" width="100%"></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        <a href="digichef-module.html">Package&nbsp;digichef</a> ::
        <a href="digichef.recommender-module.html">Package&nbsp;recommender</a> ::
        Module&nbsp;utils
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
        <tr><td align="right"><span class="options">[<a href="javascript:void(0);" class="privatelink"
    onclick="toggle_private();">hide&nbsp;private</a>]</span></td></tr>
        <tr><td align="right"><span class="options"
            >[<a href="frames.html" target="_top">frames</a
            >]&nbsp;|&nbsp;<a href="digichef.recommender.utils-module.html"
            target="_top">no&nbsp;frames</a>]</span></td></tr>
      </table>
    </td>
  </tr>
</table>
<!-- ==================== MODULE DESCRIPTION ==================== -->
<h1 class="epydoc">Module utils</h1><p class="nomargin-top"><span class="codelink"><a href="digichef.recommender.utils-pysrc.html">source&nbsp;code</a></span></p>
<!-- ==================== FUNCTIONS ==================== -->
<a name="section-Functions"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td colspan="2" class="table-header">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr valign="top">
        <td align="left"><span class="table-header">Functions</span></td>
        <td align="right" valign="top"
         ><span class="options">[<a href="#section-Functions"
         class="privatelink" onclick="toggle_private();"
         >hide private</a>]</span></td>
      </tr>
    </table>
  </td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="digichef.recommender.utils-module.html#pearson_correlation" class="summary-sig-name">pearson_correlation</a>(<span class="summary-sig-arg">v1</span>,
        <span class="summary-sig-arg">v2</span>)</span><br />
      &gt;&gt;&gt; v1=[0,10,10,0,10]...</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="digichef.recommender.utils-pysrc.html#pearson_correlation">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="digichef.recommender.utils-module.html#tanamoto2" class="summary-sig-name">tanamoto2</a>(<span class="summary-sig-arg">v1</span>,
        <span class="summary-sig-arg">v2</span>)</span><br />
      &gt;&gt;&gt; v1=['a','b','c']...</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="digichef.recommender.utils-pysrc.html#tanamoto2">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="digichef.recommender.utils-module.html#distance_matrix_p1_p2" class="summary-sig-name">distance_matrix_p1_p2</a>(<span class="summary-sig-arg">prefs_p1</span>,
        <span class="summary-sig-arg">prefs_p2</span>)</span><br />
      &gt;&gt;&gt; prefs={}...</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="digichef.recommender.utils-pysrc.html#distance_matrix_p1_p2">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="digichef.recommender.utils-module.html#get_usb_recommendations" class="summary-sig-name">get_usb_recommendations</a>(<span class="summary-sig-arg">element</span>,
        <span class="summary-sig-arg">matrix</span>)</span><br />
      Calculates recommendations for a given element by using an average of every other element's rankings.</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="digichef.recommender.utils-pysrc.html#get_usb_recommendations">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="digichef.recommender.utils-module.html#kcluster" class="summary-sig-name">kcluster</a>(<span class="summary-sig-arg">votes_matrix</span>,
        <span class="summary-sig-arg">items</span>,
        <span class="summary-sig-arg">clusters</span>)</span><br />
      Create k randomly placed centroids...</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="digichef.recommender.utils-pysrc.html#kcluster">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr class="private">
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a name="_test"></a><span class="summary-sig-name">_test</span>()</span></td>
          <td align="right" valign="top">
            <span class="codelink"><a href="digichef.recommender.utils-pysrc.html#_test">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
</table>
<!-- ==================== VARIABLES ==================== -->
<a name="section-Variables"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td colspan="2" class="table-header">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr valign="top">
        <td align="left"><span class="table-header">Variables</span></td>
        <td align="right" valign="top"
         ><span class="options">[<a href="#section-Variables"
         class="privatelink" onclick="toggle_private();"
         >hide private</a>]</span></td>
      </tr>
    </table>
  </td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="digichef.recommender.utils-module.html#path" class="summary-name">path</a> = <code title="addsitedir(abspath(join(dirname(__file__), 'site-packages')), set())">addsitedir(abspath(join(dirname(__file__), 'site-packag<code class="variable-ellipsis">...</code></code>
    </td>
  </tr>
</table>
<!-- ==================== FUNCTION DETAILS ==================== -->
<a name="section-FunctionDetails"></a>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td colspan="2" class="table-header">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr valign="top">
        <td align="left"><span class="table-header">Function Details</span></td>
        <td align="right" valign="top"
         ><span class="options">[<a href="#section-FunctionDetails"
         class="privatelink" onclick="toggle_private();"
         >hide private</a>]</span></td>
      </tr>
    </table>
  </td>
</tr>
</table>
<a name="pearson_correlation"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">pearson_correlation</span>(<span class="sig-arg">v1</span>,
        <span class="sig-arg">v2</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="digichef.recommender.utils-pysrc.html#pearson_correlation">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <pre class="literalblock">
&gt;&gt;&gt; v1=[0,10,10,0,10]
&gt;&gt;&gt; v2=[10,0,0,10,0]
&gt;&gt;&gt; pearson_correlation(v1,v2)
0.0
&gt;&gt;&gt; v2=v1
&gt;&gt;&gt; pearson_correlation(v1,v2)
1.0
&gt;&gt;&gt; v2=[0,10,0,10,0]
&gt;&gt;&gt; pearson_correlation(v1,v2)
0.41666666666666669

</pre>
  <dl class="fields">
  </dl>
</td></tr></table>
</div>
<a name="tanamoto2"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">tanamoto2</span>(<span class="sig-arg">v1</span>,
        <span class="sig-arg">v2</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="digichef.recommender.utils-pysrc.html#tanamoto2">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <pre class="literalblock">
&gt;&gt;&gt; v1=['a','b','c']
&gt;&gt;&gt; v2=['c','a','b']
&gt;&gt;&gt; tanamoto2(v1,v2)
1.0
&gt;&gt;&gt; v2=['e','f','g']
&gt;&gt;&gt; tanamoto2(v1,v2)
0.0
&gt;&gt;&gt; v2=['c','f','k','a']
&gt;&gt;&gt; tanamoto2(v1,v2)
0.40000000000000002
&gt;&gt;&gt; v2=['x','f','k','a']
&gt;&gt;&gt; tanamoto2(v1,v2)
0.16666666666666666
&gt;&gt;&gt; v2=['c','b','k','a']
&gt;&gt;&gt; tanamoto2(v1,v2)
0.75
&gt;&gt;&gt; v2=['b','g','a','t','c']
&gt;&gt;&gt; v1=['x','y','z','t','v']
&gt;&gt;&gt; tanamoto2(v1,v2)
0.1111111111111111
&gt;&gt;&gt; v1=['a','b']
&gt;&gt;&gt; tanamoto2(v1,v2)
0.40000000000000002
&gt;&gt;&gt; v1=['a','b','x','y','z']
&gt;&gt;&gt; tanamoto2(v1,v2)
0.25

</pre>
  <dl class="fields">
  </dl>
</td></tr></table>
</div>
<a name="distance_matrix_p1_p2"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">distance_matrix_p1_p2</span>(<span class="sig-arg">prefs_p1</span>,
        <span class="sig-arg">prefs_p2</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="digichef.recommender.utils-pysrc.html#distance_matrix_p1_p2">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <pre class="literalblock">
&gt;&gt;&gt; prefs={}
&gt;&gt;&gt; prefs['p1']={'item1': 0, 'item2': 0, 'item3': 10, 'item4': 10, 'item5':0}
&gt;&gt;&gt; prefs['p2']={'item1': 10, 'item2': 10, 'item3': 0, 'item4': 0, 'item5':10}
&gt;&gt;&gt; prefs['p3']={'item1': 0, 'item2': 10, 'item3': 0, 'item4': 0, 'item5':10}
&gt;&gt;&gt; prefs['p4']={'item1': 0, 'item2': 0, 'item3': 0, 'item4': 0, 'item5':10}
&gt;&gt;&gt; distance_matrix_p1_p2(prefs['p1'],prefs['p1'])
1.0
&gt;&gt;&gt; distance_matrix_p1_p2(prefs['p1'],prefs['p2'])
0.0
&gt;&gt;&gt; distance_matrix_p1_p2(prefs['p1'],prefs['p3'])
0.16666666666666669
&gt;&gt;&gt; distance_matrix_p1_p2(prefs['p1'],prefs['p4'])
0.29587585476806849
&gt;&gt;&gt; distance_matrix_p1_p2(prefs['p2'],prefs['p4'])
0.70412414523193156
&gt;&gt;&gt; distance_matrix_p1_p2(prefs['p2'],prefs['p3'])
0.83333333333333326

</pre>
  <dl class="fields">
  </dl>
</td></tr></table>
</div>
<a name="get_usb_recommendations"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">get_usb_recommendations</span>(<span class="sig-arg">element</span>,
        <span class="sig-arg">matrix</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="digichef.recommender.utils-pysrc.html#get_usb_recommendations">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <pre class="literalblock">
Calculates recommendations for a given element by using an average of every other element's rankings.
Returns a pair (value,element_id), where value is [0..X] where 0 doesn't match, and X fully matches
&gt;&gt;&gt; matrix={}
&gt;&gt;&gt; matrix['user1']={'item1':-1, 'item3': 1, 'item4': 1, 'item5':0}
&gt;&gt;&gt; matrix['user2']={'item1': 1, 'item2': 1, 'item3':-1, 'item4':-1, 'item5':10,'item6':10}
&gt;&gt;&gt; matrix['user3']={'item1':-1, 'item2': 1, 'item4':-1, 'item5':10,'item6':10,'item7':-1}
&gt;&gt;&gt; matrix['user4']={'item1':-1, 'item2':-1, 'item3':-1, 'item4':-1, 'item5':10,'item7':10,'item8':10}

</pre>
  <dl class="fields">
  </dl>
</td></tr></table>
</div>
<a name="kcluster"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">kcluster</span>(<span class="sig-arg">votes_matrix</span>,
        <span class="sig-arg">items</span>,
        <span class="sig-arg">clusters</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="digichef.recommender.utils-pysrc.html#kcluster">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <pre class="literalblock">
Create k randomly placed centroids
&gt;&gt;&gt; votes_matrix={}
&gt;&gt;&gt; votes_matrix['u1']={'it1':1,'it2':1,'it3':1,'it4':1,'it5':-1,}
&gt;&gt;&gt; votes_matrix['u2']={'it1':-1,'it2':-1,'it3':-1,'it4':-1,'it5':1,}
&gt;&gt;&gt; votes_matrix['u3']={'it1':-1,'it2':-1,'it3':-1,'it4':1,'it5':1,}
&gt;&gt;&gt; votes_matrix['u4']={'it1':1,'it2':1,'it3':1,'it4':-1,'it5':-1,}
&gt;&gt;&gt; votes_matrix['u5']={'it1':1,'it2':-1,'it3':-1,'it4':1,'it5':-1,}
&gt;&gt;&gt; items = ['it1','it2','it3','it4','it5']
&gt;&gt;&gt; clusters=2
&gt;&gt;&gt; best,centroides=kcluster(votes_matrix, items, clusters)
&gt;&gt;&gt; best
{0: ['u3', 'u2'], 1: ['u5', 'u4', 'u1']}

</pre>
  <dl class="fields">
  </dl>
</td></tr></table>
</div>
<br />
<!-- ==================== VARIABLES DETAILS ==================== -->
<a name="section-VariablesDetails"></a>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td colspan="2" class="table-header">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr valign="top">
        <td align="left"><span class="table-header">Variables Details</span></td>
        <td align="right" valign="top"
         ><span class="options">[<a href="#section-VariablesDetails"
         class="privatelink" onclick="toggle_private();"
         >hide private</a>]</span></td>
      </tr>
    </table>
  </td>
</tr>
</table>
<a name="path"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <h3 class="epydoc">path</h3>
  
  <dl class="fields">
  </dl>
  <dl class="fields">
    <dt>Value:</dt>
      <dd><table><tr><td><pre class="variable">
addsitedir(abspath(join(dirname(__file__), 'site-packages')), set())
</pre></td></tr></table>
</dd>
  </dl>
</td></tr></table>
</div>
<br />
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

      <th class="navbar" width="100%"></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Thu Apr  1 02:28:19 2010
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>
